import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.CropImageFilter;
import java.awt.image.FilteredImageSource;
import java.awt.image.ImageProducer;
import java.util.ArrayList;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JToolBar;

@SuppressWarnings("serial")
public class ForestToolbar extends JToolBar implements BlockToolbar {

	public ArrayList<TBObserver> observers = new ArrayList<TBObserver>();
	public JToolBar _toolbar;
	public int obSize = 0;
	public int imageInt;
	public boolean buildable, passable;
	public static Image[] tileset_ground = new Image[100]; // import images
	public static Image[] tileset_air = new Image[100]; // import air images

	public ForestToolbar() {
		for (int i = 0; i < tileset_ground.length; i++) {
			tileset_ground[i] = new ImageIcon("res/tileset_ground.png")
					.getImage(); // get source of image from tilesetground
			tileset_ground[i] = createImage((ImageProducer) new FilteredImageSource(
					tileset_ground[i].getSource(), new CropImageFilter(0,
							26 * i, 26, 26))); // then crop the image according
												// to i
		}

		for (int i = 0; i < tileset_air.length; i++) {
			tileset_air[i] = new ImageIcon("res/tileset_air.png").getImage(); // get
																				// source
																				// of
																				// image
																				// from
																				// tilesetair
			tileset_air[i] = createImage(new FilteredImageSource(
					tileset_air[i].getSource(), new CropImageFilter(0, 26 * i,
							26, 26))); // then crop the image according to i
		}
		_toolbar = new JToolBar();
		_toolbar.setFloatable(false);
		addButtons(_toolbar);
	}

	@Override
	public void addButtons(JToolBar toolbar) {
		JButton buttons = null;
		buttons = new JButton(new ImageIcon(tileset_ground[0]));
		buttons.setToolTipText("Passable");
		buttons.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				imageInt = 0;
				buildable = false;
				passable = true;
				notifyObservers();
			}
		});
		_toolbar.add(buttons);
		buttons = new JButton(new ImageIcon(tileset_ground[1]));
		buttons.setToolTipText("Buildable");
		buttons.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				imageInt = 1;
				buildable = true;
				passable = false;
				notifyObservers();
			}
		});
		_toolbar.add(buttons);
		buttons = new JButton(new ImageIcon(tileset_ground[2]));
		buttons.setToolTipText("Erase");
		buttons.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				imageInt = 2; // used to represent delete tile, need to be
								// changed later
				buildable = false;
				passable = false;
				notifyObservers();
			}
		});
		_toolbar.add(buttons);
	}

	@Override
	public void addObserver(TBObserver observer) {
		observers.add(observer);
		obSize++;
	}

	@Override
	public void removeObserver(TBObserver observer) {
		observers.remove(observer);
		obSize--;
	}

	@Override
	public void notifyObservers() {
		for (int i = 0; i < obSize; i++) {
			TBObserver observer = observers.get(i);
			observer.update(imageInt, buildable, passable);
		}
	}
	
	public JToolBar getToolbar(){
		return _toolbar;
	}

}